package com.gms.controller.usermanger;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gms.entity.event.Event;
import com.gms.entity.place.PlaceNotice;
import com.gms.entity.user.User;
import com.gms.service.eventmanager.EventService;
import com.gms.service.placemanager.PlaceNoticeService;
import com.gms.service.usermanager.UserService;
import com.gms.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户的Controller层
 *
 * @since2021/5/30 15:07
 */


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PlaceNoticeService placeNoticeService;
    @Autowired
    private EventService eventService;

    @RequestMapping("/index")
    public String index(Model model){
        PlaceNotice placeNotice = placeNoticeService.SelectPlaceNoticeNew();
        List<Event> eventNoticelist = eventService.queryEventNotice();
        model.addAttribute("eventNoticelist",eventNoticelist);
        model.addAttribute("placenotice",placeNotice);
        return "index";
    }

    @RequestMapping("/toChangepassword")
    public String toChangepassword(){
        return "changepassword";
    }

    @RequestMapping("/toChangeuser")
    public String toChangeuser(){
        return "changeuser";
    }

    @RequestMapping("/touser")
    public String toaddadmin(Model model){
        Page<User> page = userService.selectPage(1,1,0);
        model.addAttribute("page",page);
        return "usermanagement";
    }
    @RequestMapping("/toadmin")
    public String toadmin(Model model){
        Page<User> page = userService.selectPage(1,4,1);
        model.addAttribute("page",page);
        return "adminmanagement";
    }
    //修改密码
    @RequestMapping("/updatePassword")
    public String login(Model model,String nowpassword,String nawpassword1,HttpSession session){
        User user = new User();
        user= (User) session.getAttribute("user");
        if(user.getPassWord().equals(MD5Utils.md5(nowpassword))){
            user.setPassWord(MD5Utils.md5(nawpassword1));
            session.setAttribute("user",user);
            userService.update_password(user);
            return "redirect:/user/index";
        }else{
            model.addAttribute("msg","输入密码错误");
        }
        return "Changepassword";
    }

    //添加管理员
    @RequestMapping("/addadmin")
    public String addAdmin(Model model,Integer userId,String permission ){
        System.out.println(userId);
        User user = userService.select_one(userId);
        user.setPermission(permission);
        switch (permission){
            case "1":
                user.setRole("场地管理员");
                break;
            case "2":
                user.setRole("器材管理员");
                break;
            case "3":
                user.setRole("赛事管理员");
                break;
        }
        userService.updateUser(user);
        Page<User> page = userService.selectPage(1,1,0);
        model.addAttribute("page",page);
        return "usermanagement";
    }


    //修改用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user,HttpSession session){
        User user1 = (User) session.getAttribute("user");
        user1.setUserName(user.getUserName());
        user1.setSex(user.getSex());
        user1.setAcademy(user.getAcademy());
        session.setAttribute("user",user1);
        session.setAttribute("username",user1.getUserName());
        userService.updateUser(user1);
        return "redirect:/user/index";
    }

    //查询用户
    @RequestMapping("/queryusers")
    public String query_users(@Nullable String number,
                              @Nullable String username,
                              @Nullable String academy,
                              @Nullable Integer pageNum,
                              Model model){
        if (pageNum==null){
            pageNum=1;
        }if(!StringUtils.isEmpty(username)){
            model.addAttribute("username",username);
        }if(!StringUtils.isEmpty(username)){
            model.addAttribute("academy",academy);
        }if(!StringUtils.isEmpty(academy)){
            model.addAttribute("number",number);
        }
        Page<User> page = userService.query_users(number, username, academy,"1",pageNum);

        model.addAttribute("page",page);
        model.addAttribute("number",number);
        model.addAttribute("username",username);
        model.addAttribute("academy",academy);
        return "usermanagement";
    }

    //查询管理员
    @RequestMapping("/queryadmins")
    public String query_admins(@Nullable String number,
                              @Nullable String username,
                              @Nullable String role,
                              @Nullable Integer pageNum,
                              Model model){
        if (pageNum==null){
            pageNum=1;
        }if(!StringUtils.isEmpty(username)){
            model.addAttribute("username",username);
        }if(!StringUtils.isEmpty(role)){
            model.addAttribute("role",role);
        }if(!StringUtils.isEmpty(number)){
            model.addAttribute("number",number);
        }
        Page<User> page = userService.query_admins(number, username, role,"4",pageNum);
        model.addAttribute("page",page);
        model.addAttribute("number",number);
        model.addAttribute("username",username);
        model.addAttribute("role",role);
        return "adminmanagement";
    }

    //删除管理员
    @RequestMapping("/deleteadmin")
    public String deleteAdmin(Model model,Integer userId){
        User user = userService.select_one(userId);
        user.setPermission("0");
        user.setRole("普通用户");
        userService.updateUser(user);
        Page<User> page = userService.selectPage(1,4,1);
        model.addAttribute("page",page);
        return "adminmanagement";
    }

    //权限转移
    @RequestMapping("/transfer")
    public String transfer(Integer userID,String password,Model model,HttpSession session){
        User user1 = new User();
        User user2 = userService.select_one(userID);
        user1= (User) session.getAttribute("user");
        if(user1.getPassWord().equals(MD5Utils.md5(password))){
            user1.setPermission("0");
            user1.setRole("普通用户");
            user2.setPermission("4");
            user2.setRole("超级管理员");
            userService.transfer(user1,user2);
            return "redirect:/login";
        }else{
            model.addAttribute("msg","输入密码错误");
        }
        return "transfer";
    }

    @RequestMapping("/totransfer")
    public String totransfer(Integer userID,Model model){
        User user = userService.select_one(userID);
        model.addAttribute("user",user);
        return "transfer";
    }

}